﻿using System;
using System.Drawing.Text;

namespace FxCore.Stuff
{
    public static class Helper
    {
        private static readonly float ComparisonTolerance;

        static Helper()
        {
            ComparisonTolerance = 0.00000000F;
        }

        public static bool CompareFloat(float fv1, float fv2)
        {
            return Math.Abs(fv1 - fv2) <= Helper.ComparisonTolerance;
        }

        public static bool CompareDouble(double dl1, double dl2)
        {
            return Math.Abs(dl1 - dl2) <= Helper.ComparisonTolerance;
        }

        public static void ByteAllCopy(byte[] src, byte[] dest)
        {
            if (src == null || dest == null)
            {
                throw new ArgumentNullException();
            }

            if (src.Length > dest.Length)
            {
                throw new ArgumentException();
            }

            unsafe
            {
                fixed (byte* pSource = src, pTarget = dest)
                {
                    byte* ps = pSource, pt = pTarget;
                    for (int i = 0; i < src.Length; i++)
                    {
                        *pt = *ps;
                        ps++;
                        pt++;
                    }
                }
            }
        }

        public static void ByteOffsetCopy(byte[] source, int sourceOffset, byte[] target, int targetOffset, int count)
        {
            if ((source == null) || (target == null))
            {
                throw new ArgumentNullException();
            }
 
            if ((sourceOffset < 0) || (targetOffset < 0) || (count < 0))
            {
                throw new ArgumentException();
            }
            
            if ((source.Length - sourceOffset < count) ||
                (target.Length - targetOffset < count))
            {
                throw new ArgumentException();
            }

            unsafe
            {
                fixed (byte* pSource = source, pTarget = target)
                {
                    byte* ps = pSource + sourceOffset, pt = pTarget + targetOffset; 
                    for (int i = 0; i < count; i++)
                    {
                        *pt = *ps;
                        pt++;
                        ps++;
                    }
                }
            }
        }
    }
}
